{% extends 'base.html' %}
{% block title %}User{% endblock %}

{% block card %}
<div class="layui-card-header">
  <h2>User</h2>
</div>
{% endblock %}


{% block body %}
<fieldset class="layui-elem-field layui-field-title">
  <legend>Reset Password</legend>
  <div class="layui-field-box" style="font-size: medium;line-height: 30px;">
      <br />
      <form class="layui-form" method="post" id="MainForm">
        {{ form.csrf_token }}
        <div class="layui-form-item">
            <label class="layui-form-label" style="width:190px">{{ form.password.label }}：</label>
            <div class="layui-input-block">
                {{ form.password(class="layui-input", style="width:40%", placeholder="Input New Password") }}
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="width:190px">{{ form.password2.label }}：</label>
            <div class="layui-input-block">
                {{ form.password2(class="layui-input", style="width:40%", placeholder="Input New Password Again") }}
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block" style="margin-left:180px">
                <div class="layui-input-inline login-btn">
                    {{ form.submit(class="layui-btn", style="font-size:medium", id="reset_password") }}
                </div>
            </div>
        </div>
      </form>
  </div>
</fieldset>
{% endblock %}

{% block script %}

<script type="text/javascript">
    layui.use(['form', 'jquery', 'upload'], function () {
        var form = layui.form;
        var $ = layui.$;

        $(document).ready(function(){

            $('#reset_password').on('click',function () {

                var form_data = new FormData(document.getElementById("MainForm"));
                console.log(form_data);

                let password = form_data.get('password');
                let password2 = form_data.get('password2');

                if(!password || !password2){
                    layui.layer.msg('两次密码不能为空!', {time: 1000});
                    return false;
                };

                if(password !== password2){
                    $('#password').val('');
                    $('#password2').val('');
                    layui.layer.msg('两次密码不一致,请重新输入!', {time: 1000});
                    return false;
                };

                form_data.set('user_id', {{ record_id }})

                $.ajax({
                    url: "{{url_for('main.user_reset_password')}}",
                    type: "post",
                    data: form_data,
                    processData: false,
                    contentType: false,
                    success: result => {

                        if(!validate_api_result(result)){return}; // 此处先预处理一下返回值, 需要报错的要报错
                        layui.layer.msg('重置成功!',{time: 1000});
                        $('#reset_password').hide();
                    },
                    fail: err => {
                        layui.layer.msg('失败',{time: 1000});
                    }
                });
                return false;

            });
        });

    });



</script>


{% endblock %}